home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / shell / dialog-0.000 / dialog-0 / dialog-0.6c / guage.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-11  |  3.0 KB  |  107 lines

  1. /*
  2.  *  guage.c -- implements the guage dialog
  3.  *
  4.  *  AUTHOR: Marc Ewing, Red Hat Software
  5.  *
  6.  *  This program is free software; you can redistribute it and/or
  7.  *  modify it under the terms of the GNU General Public License
  8.  *  as published by the Free Software Foundation; either version 2
  9.  *  of the License, or (at your option) any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public License
  17.  *  along with this program; if not, write to the Free Software
  18.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. #include "dialog.h"
  22.  
  23. /*
  24.  * Display a guage, or progress meter.  Starts at percent% and
  25.  * reads stdin.  If stdin is not XXX, then it is interpreted as
  26.  * a percentage, and the display is updated accordingly.  Otherwise
  27.  * the next line is the percentage, and subsequent lines up to
  28.  * another XXX are used for the new prompt.  Note that the size
  29.  * of the window never changes, so the prompt can not get any
  30.  * larger than the height and width specified.
  31.  */
  32. int
  33. dialog_guage (const char *title, const char *prompt, int height,
  34.         int width, int percent)
  35. {
  36.     int i, x, y;
  37.     char buf[1024];
  38.     char prompt_buf[1024];
  39.     WINDOW *dialog;
  40.  
  41.     /* center dialog box on screen */
  42.     x = (COLS - width) / 2;
  43.     y = (LINES - height) / 2;
  44.  
  45. #ifdef HAVE_NCURSES
  46.     if (use_shadow)
  47.     draw_shadow (stdscr, y, x, height, width);
  48. #endif
  49.     dialog = newwin (height, width, y, x);
  50.     keypad (dialog, TRUE);
  51.  
  52.     do {
  53.     werase (dialog);
  54.     draw_box (dialog, 0, 0, height, width, dialog_attr, border_attr);
  55.  
  56.     if (title != NULL) {
  57.         wattrset (dialog, title_attr);
  58.         wmove (dialog, 0, (width - strlen (title)) / 2 - 1);
  59.         waddch (dialog, ' ');
  60.         waddstr (dialog, title);
  61.         waddch (dialog, ' ');
  62.     }
  63.     wattrset (dialog, dialog_attr);
  64.     print_autowrap (dialog, prompt, width - 2, 1, 2);
  65.  
  66.     draw_box (dialog, height - 4, 3, 3, width - 6, dialog_attr,
  67.           border_attr);
  68.  
  69.     wmove (dialog, height - 3, 4);
  70.     wattrset (dialog, title_attr);
  71.     for (i = 0; i < (width - 8); i++)
  72.         waddch (dialog, ' ');
  73.  
  74.     wattrset (dialog, title_attr);
  75.     wmove (dialog, height - 3, (width / 2) - 2);
  76.     sprintf (buf, "%3d%%", percent);
  77.     waddstr (dialog, buf);
  78.  
  79.     x = (percent * (width - 8)) / 100;
  80.     wattrset (dialog, item_selected_attr);
  81.     wmove (dialog, height - 3, 4);
  82.     for (i = 0; i < x; i++)
  83.         waddch (dialog, winch (dialog));
  84.  
  85.     wrefresh (dialog);
  86.  
  87.     if (feof (stdin))
  88.         break;
  89.     gets (buf);
  90.     if (buf[0] == 'X') {
  91.         /* Next line is percentage */
  92.         gets (buf);
  93.         percent = atoi (buf);
  94.  
  95.         /* Rest is message text */
  96.         prompt_buf[0] = '\0';
  97.         while (strncmp (gets (buf), "XXX", 3))
  98.         strcat (prompt_buf, buf);
  99.         prompt = prompt_buf;
  100.     } else
  101.         percent = atoi (buf);
  102.     } while (1);
  103.  
  104.     delwin (dialog);
  105.     return (0);
  106. }
  107.